package geom;

import geom.Geom;
import geom.PolarVector;
import geom.Vector;
import processing.core.PApplet;
public class FieldFollower {
	float angle;
	float speed;
	Vector pos;
	AngleField field;
	public FieldFollower(Vector pos, AngleField field){
		this.pos = pos;
		speed = 2;
		angle = (float)Math.random()*2f*(float)Math.PI;
		this.field = field;
	}
	
	
	public void draw(PApplet pa){
		Vector oldPos = pos;
		PolarVector v = field.value(pos).getPolar();
		float fieldAngle = v.a;
		//if(Geom.getShortestAngleDistance(v.a, angle) > 
		//   Geom.getShortestAngleDistance(v.a+Geom.PI, angle))
		//   fieldAngle = Geom.normalizeAngle(v.a+Geom.PI);
		angle = angle * (1 - (float)Math.min(v.d, 1)) + fieldAngle * (float)Math.min(v.d, 1);
		pos = pos.add(new PolarVector(angle, 0, speed).getCartesian());
		pa.line(oldPos.x, oldPos.y, pos.x, pos.y);
	}
}
